home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / cicnshowinit / cicnshowinit.c next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  5.0 KB  |  190 lines  |  [TEXT/R*ch]

  1. /***********************************************************
  2.    This is the code for ShowINIT as printed in Dave Mark's
  3.    great book 'Mac Programming Primer' along with some
  4.    changes to use 'cicn' resources and some other trivialities.
  5.  
  6.    Modified by Tom Bridgwater 2/16/1994 (see //TB comments)
  7.    (please give me credit if you do anything significant 
  8.     with this -- Thanks  tbridgwa@cymbal.calpoly.edu)
  9.    [first used in my QuietStart 1.2 freeware extension]
  10.        1. Always uses 'cicn' for color and B/W
  11.        2. Displacement parameters for icon positioning
  12.        3. Debugging support
  13.           (normal application procedure if _TEST defined)
  14.  
  15. ************************************************************
  16.         typical call :
  17. ************************************************************
  18.  
  19. Handle    procH;
  20.  
  21.     if((procH = GetResource('PROC', PROC_ID)) != 0L)
  22.     {
  23.         HLock(procH);
  24. //TB: displacement parameters:
  25.         CallPascal(ICON_ID, -1, 0, 0, *procH);
  26.         HUnlock(procH);
  27.     }
  28.  
  29. ***********************************************************/
  30.  
  31. // #include <color.h>
  32.  
  33. typedef struct QuickDraw
  34. {    /* struct to hold QuickDraw Vriables */
  35.     char    private[76];
  36.     long    randSeed;
  37.     BitMap    screenBits;
  38.     Cursor    arrow;
  39.     Pattern    dkGray;
  40.     Pattern    ltGray;
  41.     Pattern    gray;
  42.     Pattern    black;
  43.     Pattern    white;
  44.     GrafPtr    thePort;
  45. } QuickDraw;
  46.  
  47. extern short    myH        : 0x92c;    /* CurApName + 28 */
  48. extern short    myCheck    : 0x92E;    /* CurApName + 30 */
  49.  
  50. #define firstX            8
  51.     /* left margin - offset to first icon */
  52.  
  53. #define bottomEdge        8
  54.     /* this far from bottom of screen */
  55.  
  56. //TB: iconWidth is taken from 'cicn', not #defined
  57.  
  58. #define defaultMoveX    40
  59.     /* default amount to move icons */
  60.  
  61. #define checksumConst    0x1021
  62.     /* constant used for computing checksum */
  63.  
  64. #define minColorDepth    4
  65.     /* min. bits/pixel for drawing color icon */
  66.  
  67. //TB: access to 'cicn' data
  68. #define dataIndex        6
  69.     /* long index of byte offset to mask & B/W data in cicn */
  70.     
  71. //TB: access to 'cicn' data
  72. #define maskOffset        10
  73.     /* byte offset to mask data from beginning of data [dataIndex] */
  74.  
  75. #define iconRowBytes    4
  76.     /* 32/8 bits */
  77.  
  78. //TB: access to 'cicn' data
  79. #define cicnRowBytesOffset    0x1b
  80.     /* index of short in resource */
  81.  
  82. #define hasCQDBit        6
  83.     /* bit in ROM85 cleared if CQD available */
  84.  
  85. //TB: procedure name not main() while debugging
  86. //TB: displacement parameters
  87. #ifdef _TEST
  88. pascal void showInit(iconID, moveX, dispX, dispY)
  89. #else
  90. pascal void main(iconID, moveX, dispX, dispY)
  91. #endif _TEST
  92. short    iconID, moveX, dispX, dispY;
  93. {
  94. Handle        theIconHdl;
  95. short        dh, colorFlag, theDepth;
  96. GDHandle    theMainDevice;
  97. Rect        srcRect, destRect;
  98. BitMap        myBitMap;
  99. GrafPort    myPort;
  100. QuickDraw    qdGlobals;
  101. Ptr            localA5, savedA5;
  102.  
  103. //TB: don't setup A5 while debugging
  104. #ifndef _TEST
  105.     asm
  106.     {
  107.         move.l    A5, savedA5
  108.         lea        localA5,A5
  109.         move.l    A5, CurrentA5;
  110.     }
  111. #endif _TEST
  112.  
  113.     InitGraf(&qdGlobals.thePort);
  114.     OpenPort(&myPort);
  115.     colorFlag = 0;
  116.     
  117.     if (!(BitTst(&ROM85, 7-hasCQDBit)))
  118.     {
  119.         theMainDevice = MainDevice;
  120.         theDepth = (*(*theMainDevice)->gdPMap)->pixelSize;
  121.         if (theDepth >= minColorDepth)
  122.             if((theIconHdl = (Handle)GetCIcon(iconID)) != 0L)
  123.                 colorFlag = 1;
  124.     }
  125.     
  126.     if (!colorFlag)
  127. //TB: load 'cicn' instead of 'ICN#' for B/W
  128.         if (!(theIconHdl = GetResource('cicn', iconID)))
  129.         {
  130.             SysBeep(3);
  131.             goto out;
  132.         }
  133.     dh = (myH << 1) ^ checksumConst;
  134.     myH = ((dh == myCheck) ? (myH):(firstX));
  135.     
  136. //TB: include displacement in destination
  137.     destRect.bottom = myPort.portRect.bottom - bottomEdge - dispY;
  138.     destRect.left = myPort.portRect.left + myH + dispX;
  139. //TB: account for variable size 'cicn' in destination
  140.     destRect.top = destRect.bottom -
  141.                     (    (*(CIconHandle)theIconHdl)->iconPMap.bounds.bottom
  142.                      -    (*(CIconHandle)theIconHdl)->iconPMap.bounds.top );
  143.     destRect.right = destRect.left + 
  144.                     (    (*(CIconHandle)theIconHdl)->iconPMap.bounds.right
  145.                      -    (*(CIconHandle)theIconHdl)->iconPMap.bounds.left);
  146.  
  147.     if (colorFlag)
  148.     {
  149.         PlotCIcon(&destRect, (CIconHandle)theIconHdl);
  150.         DisposCIcon((CIconHandle)theIconHdl);
  151.     } else
  152.     {
  153.         HLock(theIconHdl);
  154.         srcRect.top = srcRect.left = 0;
  155. //TB: account for variable size 'cicn' in source
  156.         srcRect.bottom = destRect.bottom - destRect.top;
  157.         srcRect.right = destRect.right - destRect.left;
  158. //TB: account for variable size 'cicn' in BitMap parameters
  159.         myBitMap.rowBytes = ((short*)(*theIconHdl))[cicnRowBytesOffset];
  160.         myBitMap.bounds = srcRect;
  161. //TB: use 'cicn' data to find mask
  162.         myBitMap.baseAddr = *theIconHdl + ((long*)*theIconHdl)[dataIndex] + maskOffset;
  163.         /* punch hole with mask */
  164.         CopyBits(&myBitMap, &myPort.portBits, &srcRect, &destRect, srcBic, 0L);
  165. //TB: use 'cicn' data to find B/W icon
  166.         /* icon immideatly follows mask, so add size of mask to get icon baseAddr */
  167.         myBitMap.baseAddr += myBitMap.rowBytes * srcRect.bottom;
  168.         /* now draw the icon */
  169.         CopyBits (&myBitMap, &myPort.portBits, &srcRect, &destRect, srcOr, 0L);
  170.         HUnlock(theIconHdl);
  171.         ReleaseResource(theIconHdl);
  172.     }
  173.     myH += ((moveX == -1) ? (defaultMoveX) : (moveX));
  174.     myCheck = (myH << 1) ^ checksumConst;
  175.     
  176. out:
  177.     ClosePort(&myPort);
  178.  
  179. //TB: don't restore A5 while debugging
  180. #ifndef _TEST
  181.     asm
  182.     {
  183.         move.l    savedA5, A5
  184.         move.l    A5, CurrentA5
  185.     }
  186. #endif _TEST
  187. }
  188.  
  189.         
  190.